<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <meta name="description" content="Clamp a model to a 3D Tileset using the clampToHeight function.">
    <meta name="cesium-sandcastle-labels" content="Showcases, 3D Tiles">
    <title>Cesium Demo</title>
    <script type="text/javascript" src="../Sandcastle-header.js"></script>
    <script type="text/javascript" src="../../../Build/CesiumUnminified/Cesium.js" nomodule></script>
    <script type="module" src="../load-cesium-es6.js"></script>
</head>
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
    @import url(../templates/bucket.css);
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay"><h1>Loading...</h1></div>
<div id="toolbar"></div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
    'use strict';
//Sandcastle_Begin
var viewer = new Cesium.Viewer('cesiumContainer', {
    terrainProvider : Cesium.createWorldTerrain()
});
var scene = viewer.scene;
var clock = viewer.clock;

var entity;
var positionProperty;
var dataSourcePromise = Cesium.CzmlDataSource.load('../../SampleData/ClampToGround.czml');
viewer.dataSources.add(dataSourcePromise).then(function(dataSource) {
    entity = dataSource.entities.getById('CesiumMilkTruck');
    positionProperty = entity.position;
});

var tileset = scene.primitives.add(
    new Cesium.Cesium3DTileset({
        url: Cesium.IonResource.fromAssetId(40866)
    })
);

viewer.camera.setView({
    destination: new Cesium.Cartesian3(1216403.8845586285, -4736357.493351395, 4081299.715698949),
    orientation: new Cesium.HeadingPitchRoll(4.2892217081808806, -0.4799070147502502, 6.279789177843313),
    endTransform : Cesium.Matrix4.IDENTITY
});

if (scene.clampToHeightSupported) {
    tileset.initialTilesLoaded.addEventListener(start);
} else {
    window.alert('This browser does not support clampToHeight.');
}

function start() {
    clock.shouldAnimate = true;
    var objectsToExclude = [entity];
    scene.postRender.addEventListener(function() {
        var position = positionProperty.getValue(clock.currentTime);
        entity.position = scene.clampToHeight(position, objectsToExclude);
    });
}
//Sandcastle_End
    Sandcastle.finishedLoading();
}
if (typeof Cesium !== 'undefined') {
    window.startupCalled = true;
    startup(Cesium);
}
</script>
</body>
</html>
